package csc.triangulation;


/*
 * Represents triangle with its 3 points.
 * @author Stanislau Dubrouski
 */
public class Triangle {

    private Vector3d p1;
    private Vector3d p2;
    private Vector3d p3;

    public Triangle() {
    }

    public Triangle(Vector3d p1, Vector3d p2, Vector3d p3) {
        if (p1 == null || p2 == null || p3 == null) {
            throw new IllegalArgumentException("point cannot be null.");
        }
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public Vector3d getP1() {
        return p1;
    }

    public void setP1(Vector3d p1) {
        this.p1 = p1;
    }

    public Vector3d getP2() {
        return p2;
    }

    public void setP2(Vector3d p2) {
        this.p2 = p2;
    }

    public Vector3d getP3() {
        return p3;
    }

    public void setP3(Vector3d p3) {
        this.p3 = p3;
    }

    /**
     * Checks if given triangle shares at least on of vertexes of this'es vertexes.
     * @param other triangle to check for vertex sharing
     * @return true if at least 1 of vertexes is shared between given triangles
     */
    public boolean sharesVertex(Triangle other) {
        return p1 == other.p1 || p1 == other.p2 || p1 == other.p3
                || p2 == other.p1 || p2 == other.p2 || p2 == other.p3
                || p3 == other.p1 || p3 == other.p2 || p3 == other.p3;
    }
    
    @Override
    public String toString() {
        return "Triangle{" + "p1=" + p1 + ", p2=" + p2 + ", p3=" + p3 + '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Triangle other = (Triangle) obj;
        if (this.p1 != other.p1 && (this.p1 == null || !this.p1.equals(other.p1))) {
            return false;
        }
        if (this.p2 != other.p2 && (this.p2 == null || !this.p2.equals(other.p2))) {
            return false;
        }
        if (this.p3 != other.p3 && (this.p3 == null || !this.p3.equals(other.p3))) {
            return false;
        }
        return true;
    }



    @Override
    public int hashCode() {
        int hash = 7;
        hash = 79 * hash + (this.p1 != null ? this.p1.hashCode() : 0);
        hash = 79 * hash + (this.p2 != null ? this.p2.hashCode() : 0);
        hash = 79 * hash + (this.p3 != null ? this.p3.hashCode() : 0);
        return hash;
    }
}
